export function isObject (input) {
  return Object.prototype.toString.call(input) === '[object Object]'
}

export function isArray(input) {
  return input instanceof Array || Object.prototype.toString.call(input) === '[object Array]'
}

export function isDate(input) {
  return input instanceof Date || Object.prototype.toString.call(input) === '[object Date]'
}

export function isNumber(input) {
  return input instanceof Number || Object.prototype.toString.call(input) === '[object Number]'
}

export function isString(input) {
  return input instanceof String || Object.prototype.toString.call(input) === '[object String]'
}

export function isBoolean(input) {
  return typeof input === 'boolean'
}

export function isFunction(input) {
  return typeof input === 'function'
}

export function deepCopy(data) {
  let copyOne = null
  if (isObject(data)) {
    copyOne = {}
    for (const key in data) {
      copyOne[key] = deepCopy(data[key])
    }
  } else if (isArray(data)) {
    copyOne = []
    for (const index of data) {
      copyOne.push(deepCopy(index))
    }
  } else {
    copyOne = data
  }
  return copyOne
}

export function deepFreeze(obj) {
  Object.freeze(obj)
  Object.keys(obj).forEach((key, value) => {
    if (isObject(obj[key])) {
      deepFreeze(obj[key])
    }
  })
  return obj
}

export function mergeArray(arr1, arr2) {
  for (let i = 0; i < arr1.length; i++) {
    for (let j = 0; j < arr2.length; j++) {
      if (arr1[i] === arr2[j]) {
        arr1.splice(i, 1)
      }
    }
  }
  for (let i = 0; i < arr2.length; i++) {
    arr1.push(arr2[i])
  }
  return arr1
}

export function add (arg1, arg2) {
  let s1 = arg1.toString()
  let s2 = arg2.toString()
  let arg1Arr = s1.split('.')
  let arg2Arr = s2.split('.')
  let d1 = arg1Arr.length === 2 ? arg1Arr[1] : ''
  let d2 = arg2Arr.length === 2 ? arg2Arr[1] : ''
  let maxLen = Math.max(d1.length, d2.length)
  let m = Math.pow(10, maxLen)
  return Number(((s1 * m + s2 * m) / m).toFixed(maxLen))
}

export function sub(arg1, arg2) {
  return add(arg1, -arg2)
}

export function mul(arg1, arg2) {
  let m = 0
  let s1 = arg1.toString()
  let s2 = arg2.toString()
  try { m += s1.split('.')[1].length } catch (e) {}
  try { m += s2.split('.')[1].length } catch (e) {}
  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}

export function div(arg1, arg2) {
  let t1 = 0
  let t2 = 0
  try { t1 = arg1.toString().split('.')[1].length } catch (e) {}
  try { t2 = arg2.toString().split('.')[1].length } catch (e) {}
  let r1 = Number(arg1.toString().replace('.', ''))
  let r2 = Number(arg2.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}

export function clearEmpty (data) {
  let outData = {}
  for (let key in data) {
    const item = data[key]
    const isArrEmpty = isArray(item) && item.length === 0
    const isObjEmpty = isObject(item) && Object.keys(item).length === 0
    if (!item) { continue }
    if (isArrEmpty) { continue }
    if (isObjEmpty) { continue }
    outData[key] = item
  }
  return outData
}
